package com.wgj.android.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.BoolRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jiaai.xui.XUI;
import com.jiaai.xui.utils.XEmptyUtils;
import com.jiaai.xui.utils.XRegexUtils;
import com.jiaai.xui.utils.XThemeUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.http.WGJAPI;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/5.
 */

public class RegisterActivity extends AppCompatActivity {
    @BindView(R.id.phone)
    EditText phone;


    @BindView(R.id.sendCode)
    TextView sendCodeBut;

    @BindView(R.id.codeEdit)
    EditText codeEdit;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.register_activity);
        ButterKnife.bind(this);
    }


    @OnClick(R.id.registerBut) void  register(View view){
        if(!XRegexUtils.checkMobile(phone.getText().toString())){
              XToast.error("请输入正确的手机的!");
              return;
        }
        if(XEmptyUtils.isSpace(codeEdit.getText().toString())){
            XToast.error("请输入验证码!");
            return;
        }
        checkCode();
    }

    public void register(){
        Map<String,Object> params=new HashMap<>();
        params.put("mPhone",phone.getText().toString());
        XHttp.obtain().post(WGJAPI.REGISTER_URL, params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                XToast.success("注册成功");
                finish();
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("注册失败");
            }
        });
    }

    public  void checkCode(){
        XHttp.obtain().get(WGJAPI.SEND_VALIDATA_CODE+"?phone="+phone.getText().toString()+"&code="+codeEdit.getText().toString(), null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                register();

            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("注册失败");
            }
        });
    }


    @OnClick(R.id.sendCode) void  onSendCodeBut(View view){
        if(!XRegexUtils.checkMobile(phone.getText().toString())){
            XToast.error("请输入正确的手机的!");
            return;
        }

         XHttp.obtain().get(WGJAPI.SEND_CODE +"?phone=" + phone.getText().toString(), null, new HttpCallBack<String>() {
             @Override
             public void onSuccess(String s) {
                 XToast.success("验证码发送成功");
                 new CountDownTimerUtils(60000,1000).start();
                 sendCodeBut.setClickable(false); //设置不可点击
                 sendCodeBut.setTextColor(XUI.getColor(R.color.secondary_text));
             }

             @Override
             public void onFailed(int i, String s, boolean b) {
                 XToast.success("验证码发送失败");
             }
         });
    }



    public class CountDownTimerUtils extends CountDownTimer {


        public CountDownTimerUtils(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {
            sendCodeBut.setText("重新发送("+millisUntilFinished / 1000+")");  //设置倒计时时间
        }


        @Override
        public void onFinish() {
            sendCodeBut.setText("获取验证码");
            sendCodeBut.setTextColor(XThemeUtils.getColorPrimary(RegisterActivity.this));
            sendCodeBut.setClickable(true);//重新获得点击
        }
    }
}
